package com.lx.boot.web.config;

import com.lx.annotation.Note;
import com.lx.boot.config.annotation.Properties;
import com.lx.util.LX;
import lombok.Getter;

import static com.lx.constant.DefaultBaseConstant.DEFAULT_SERVER_EXCLUDE_URL;

@Getter
@Properties(prefix = "server.web")
public class WebProperties {

    @Note("无需登录接口")
    private String notAuthUrlPattern;

    @Note("无需加密的接口列表")
    private String notEncryptionUrlPattern;

    @Note("无需登录和加密的接口")
    private String notAuthAndEncryptionUrlPattern;

    @Note("三方api接口地址")
    private String apiSecurityFilterUrlPattern;

    @Note("过滤器排除的路径")
    private String excludeUrl;

    @Note("是否允许跨域")
    private boolean allowCrossdomain;

    @Note("是否进行csrf检查")
    private boolean csrf;

    @Note("firmId是否必填")
    private boolean firmIdRequired;

    @Note("指定使用什么接口过滤器 md5ApiFilter, rsaAesEncryptApiFilter")
    private String apiFilterName = "defaultApiFilter";

    @Note("打印请求头信息")
    private boolean logHeader;

    @Note("对返回值加密")
    private boolean returnEncryption;

    @Note("判断当前url是否不需要登录和验证签名")
    public boolean isNotAuthAndEncryptionUrl(String url){
        return LX.isNotEmpty(notAuthAndEncryptionUrlPattern) && url.matches(notAuthAndEncryptionUrlPattern);
    }

    @Note("无需判断加密的接口")
    public boolean isNotEncryptionUrl(String url){
        if (LX.isNotEmpty(notEncryptionUrlPattern) && url.matches(notEncryptionUrlPattern)){
            return true;
        }
        return isNotAuthAndEncryptionUrl(url);
    }

    @Note("无需登录的接口")
    public boolean isNotAuthUrl(String url){
        if (LX.isNotEmpty(notAuthUrlPattern) && url.matches(notAuthUrlPattern)){
            return true;
        }
        return isNotAuthAndEncryptionUrl(url);
    }


    @Note("过滤器排除的路径")
    public boolean isExcludeUrl(String url){
        return url.matches(DEFAULT_SERVER_EXCLUDE_URL) || (LX.isNotEmpty(excludeUrl) && url.matches(excludeUrl));
    }


}
